function Create2DArray(rows) {
  var arr = [];

  for (var i=-1; i<rows; i++) {
     arr[i] = [];
  }

  return arr;
}

function addSlashes(str) {
    return str.replace(/\\/g, '\\\\')
              .replace(/\'/g, '\\\'')
              .replace(/\"/g, '\\"')
              .replace(/\0/g, '\\0');
}

function checkTouchingMines(xx,yy) {
		var ret = 0;
		(mf.field[xx-1][yy-1] == "X") ? ret++ : ret;
		(mf.field[xx][yy-1] == "X") ? ret++ : ret;
		(mf.field[xx+1][yy-1] == "X") ? ret++ : ret;
		(mf.field[xx-1][yy] == "X") ? ret++ : ret;
		(mf.field[xx+1][yy] == "X") ? ret++ : ret;
		(mf.field[xx-1][yy+1] == "X") ? ret++ : ret;
		(mf.field[xx][yy+1] == "X") ? ret++ : ret;
		(mf.field[xx+1][yy+1] == "X") ? ret++ : ret;
	return ret;
}

function initializeGame() {
	window.mf = {};
	
	mf.field = Create2DArray(11);
	for (yy=-1; yy<11; yy++)
	{
		for (xx=-1; xx<11; xx++)
		{
			mf.field[xx][yy] = "";
		}
	}
	
	for (bombs=0; bombs<10; bombs++)
	{
		do {
			zx = Math.round(Math.random()*9);
			zy = Math.round(Math.random()*9);
		} while (mf.field[zx][zy] == "X");
		
		mf.field[zx][zy] = "X";
	}

	for (yy=0; yy<10; yy++)
	{
		for (xx=0; xx<10; xx++)
		{
			if (mf.field[xx][yy] != "X") {
				mf.field[xx][yy] = checkTouchingMines(xx,yy);
			}
		}
	}
}